package com.example.demo.strategy;

import org.springframework.stereotype.Component;

// 策略接口
interface Strategy {
    double doOperation(double num1, double num2);
}

// 加法策略类
class OperationAdd implements Strategy {
    @Override
    public double doOperation(double num1, double num2) {
        return num1 + num2;
    }
}

// 减法策略类
class OperationSubtract implements Strategy {
    @Override
    public double doOperation(double num1, double num2) {
        return num1 - num2;
    }
}

// 上下文类
@Component
public class Context {
    public double executeStrategy(Strategy strategy, double num1, double num2) {
        return strategy.doOperation(num1, num2);
    }
}    